package com.xiashitech.agent.service.extload;

import com.xiashitech.agent.config.StartConfig;
import com.xiashitech.agent.db.dao.extload.DataWaterMarkConfigDao;
import com.xiashitech.agent.db.dao.extload.DesensitiseRuleDao;
import com.xiashitech.agent.db.dao.extload.UserPermissionDao;
import com.xiashitech.agent.db.entity.DataWaterMarkConfig;
import com.xiashitech.agent.db.entity.DesensitiseRule;
import com.xiashitech.agent.db.entity.UserPermission;
import com.xiashitech.agent.utils.XSUtil;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class AuthQueryService {

    public List<UserPermission> queryPermissions(String userId, String schemaName, String tableName, String operate) {
        if (XSUtil.isEmpty(userId) ||XSUtil.isEmpty(schemaName) || XSUtil.isEmpty(tableName) || XSUtil.isEmpty(operate)){
            XSUtil.error("[queryPermissions] args check invalid.");
            return null;
        }
        List<UserPermission> userPermissionList = null;
        try {
            userPermissionList = userPermissionDao.list(userId, schemaName, tableName, operate);
        } catch (SQLException e) {
            XSUtil.error("[queryPermissions] {}", e.getMessage());
            StartConfig.throwableStackPrinter(e);
        }
        return userPermissionList;
    }

    public List<DesensitiseRule> queryDesensitizeInfo(String schemaName, String tableName, String colName) {
        List<DesensitiseRule> desensitiseRuleList = new ArrayList<>();
        if (XSUtil.isEmpty(schemaName) || XSUtil.isEmpty(tableName) || XSUtil.isEmpty(colName)){
            XSUtil.error("[ERROR] [queryDesensitizeInfo] args check invalid.");
            return desensitiseRuleList;
        }
        try {
            desensitiseRuleList = desensitiseRuleDao.list(schemaName, tableName, colName);
        } catch (SQLException e) {
            XSUtil.error("SQL ERROR {}", e.getMessage());
            StartConfig.throwableStackPrinter(e);
        }
        return desensitiseRuleList;
    }

    /**
     * 根据apiPath查询水印配置信息,由于apiPath在数据库中唯一,所以有且仅有一条配置匹配
     * @param appName
     * @param apiPath
     * @return
     */
    public DataWaterMarkConfig findDataWaterMarkConfig(String appName, String apiPath) {
        try {
            return dataWaterMarkConfigDao.find(appName, apiPath);
        } catch (SQLException e) {
            XSUtil.error("SQL ERROR {}", e.getMessage());
            StartConfig.throwableStackPrinter(e);
        }
        return null;
    }

    private volatile static AuthQueryService instance;
    private final UserPermissionDao userPermissionDao;
    private final DesensitiseRuleDao desensitiseRuleDao;
    private final DataWaterMarkConfigDao dataWaterMarkConfigDao;

    private AuthQueryService(UserPermissionDao userPermissionDao, DesensitiseRuleDao desensitiseRuleDao, DataWaterMarkConfigDao dataWaterMarkConfigDao) {
        this.userPermissionDao = userPermissionDao;
        this.desensitiseRuleDao = desensitiseRuleDao;
        this.dataWaterMarkConfigDao = dataWaterMarkConfigDao;
    }

    public static AuthQueryService getInstance() {
        if (instance == null) {
            synchronized (AuthQueryService.class) {
                if (instance == null) {
                    instance = new AuthQueryService(new UserPermissionDao(), new DesensitiseRuleDao(), new DataWaterMarkConfigDao());
                }
            }
        }
        return instance;
    }

}
